package com.slcf.controller;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.slcf.common.ErrorEnum;
import com.slcf.controller.vo.ResultVO;
import com.slcf.service.WeChatService;

/**
 * 微信交互入口
 * @author Administrator
 *
 */
@RequestMapping("/wx")
@Controller
public class WeChatController {

	@Autowired
	private WeChatService weChatService;
	
	/**
	 * 获取微信配置信息
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/getConfig.do", method = RequestMethod.GET)
	public void guessHero(HttpServletRequest request, HttpServletResponse response) {
		ResultVO result = null;
		try {
			String url = new String((request.getParameter("url")).getBytes("iso-8859-1"),"utf-8");
			if (StringUtils.isEmpty(url)) {
				result = ErrorEnum.NICKNAME_NULL.getResult();
			}
			Map<String, String> data = weChatService.getWxConfig(url);
			result = ErrorEnum.SUCCESS.getResult();
			result.setData(data);
			response.setContentType("application/json;charset=UTF-8");//防止数据传递乱码
			response.getWriter().write(JSON.toJSONString(result));
			response.getWriter().flush();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
